package com.hitqz.robot;

import com.google.common.collect.Lists;
import com.hitqz.robot.api.business.enums.BrandEnum;
import com.hitqz.robot.api.business.enums.FormConfigEnum;
import com.hitqz.robot.api.business.enums.RobotParamsFormFieldEnum;
import com.hitqz.robot.api.common.dto.form.FormConfigDto;
import com.hitqz.robot.api.common.dto.form.FormFieldConfigDto;
import com.hitqz.robot.camera.IEquipmentService;
import com.hitqz.robot.camera.configure.EquipmentCmdEnum;
import com.hitqz.robot.camera.dto.*;
import com.hitqz.robot.util.PlcToolKit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author xupkun
 * @date 2024/10/4
 */
@Slf4j
@Service("OilPlcModuleService")
public class OilPlcModuleService implements IEquipmentService {


    @Override
    public BrandEnum brand() {
        return BrandEnum.OIL_PLC;
    }

    @Override
    public boolean intSdk() {
        return true;
    }

    @Override
    public boolean login(EquipmentLoginParamsDto dto) {
        PlcToolKit.login(dto.getIp(), dto.getPort());
        return true;
    }

    @Override
    public boolean loginout(String ip) {
        return IEquipmentService.super.loginout(ip);
    }

    @Override
    public IEquipmentCmdResult executeCmd(QueryParamsDto dto, boolean async, EquipmentCmdEnum cmdEnum, IEquipmentCmdParamsDto cmdParamsDto) {
        switch (cmdEnum) {
            case RESET -> {
                PlcToolKit.writeRegister(dto.getIp(), dto.getPort(), 3, 0);
                PlcToolKit.writeRegister(dto.getIp(), dto.getPort(), 1, 1);
            }
            case STOP -> {
                PlcToolKit.writeRegister(dto.getIp(), dto.getPort(), 3, 0);
                PlcToolKit.writeRegister(dto.getIp(), dto.getPort(), 3, 1);
            }
            case UN_STOP -> {
                PlcToolKit.writeRegister(dto.getIp(), dto.getPort(), 3, 0);
            }
            case ERROR_RESET -> {
                PlcToolKit.writeRegister(dto.getIp(), dto.getPort(), 3, 0);
                PlcToolKit.writeRegister(dto.getIp(), dto.getPort(), 5, 1);
            }
            case PLC_DOOR_CTRL -> {
                PlcToolKit.writeRegister(dto.getIp(), dto.getPort(), 3, 0);
                DoorCtrlDto doorCtrlDto = (DoorCtrlDto) cmdParamsDto;
                PlcToolKit.writeRegister(dto.getIp(), dto.getPort(), 10, doorCtrlDto.getDoor());
                if (async) {
                    while (true) {
                        try {
                            TimeUnit.SECONDS.sleep(2);
                        }catch (Exception e) {

                        }
                        int fetOil = PlcToolKit.readSingleRegister(dto.getIp(), dto.getPort(), 11);
                        if (fetOil == doorCtrlDto.getDoor()) {
                            break;
                        }
                    }
                }
            }
            case START_MATCH -> {
                PlcToolKit.writeRegister(dto.getIp(), dto.getPort(), 3, 0);
                PlcToolKit.writeRegister(dto.getIp(), dto.getPort(), 13, 0);
                PlcToolKit.writeRegister(dto.getIp(), dto.getPort(), 12, 1);
                if (async) {
                    try {
                        TimeUnit.SECONDS.sleep(2);
                    }catch (Exception e) {

                    }
                    while (true) {
                        int match = PlcToolKit.readSingleRegister(dto.getIp(), dto.getPort(), 13);
                        if (match == 1) {
                            log.info("取油柜对接完成");
                            //PlcToolKit.writeRegister(dto.getIp(), dto.getPort(), 13, 0);
                            break;
                        }
                    }

                }
            }
            case MATCH_EXIT -> {
                PlcToolKit.writeRegister(dto.getIp(), dto.getPort(), 3, 0);
                PlcToolKit.writeRegister(dto.getIp(), dto.getPort(), 17, 0);
                PlcToolKit.writeRegister(dto.getIp(), dto.getPort(), 16, 1);
                if (async) {
                    try {
                        TimeUnit.SECONDS.sleep(2);
                    }catch (Exception e) {

                    }
                    while (true) {
                        int match = PlcToolKit.readSingleRegister(dto.getIp(), dto.getPort(), 17);
                        if (match == 1) {
                            //PlcToolKit.writeRegister(dto.getIp(), dto.getPort(), 17, 0);
                            log.info("取油柜模组退出完成");
                            break;
                        }
                    }

                }
            }
            case LEFT_RIGHT -> {
                // 左右传感器
                LeftRightReachDto leftRightReachDto = (LeftRightReachDto) cmdParamsDto;
                PlcToolKit.writeRegister(dto.getIp(), dto.getPort(), 18, leftRightReachDto.getLeft());
                PlcToolKit.writeRegister(dto.getIp(), dto.getPort(), 19, leftRightReachDto.getRight());
            }
            case FET_OIL -> {
                PlcToolKit.writeRegister(dto.getIp(), dto.getPort(), 7, 0);
                //PlcToolKit.writeRegister(dto.getIp(), dto.getPort(), 15, 0);
                FetchOilDto fetchOilDto = (FetchOilDto) cmdParamsDto;
                PlcToolKit.writeRegister(dto.getIp(), dto.getPort(), 15, fetchOilDto.getSn());
            }


        }




        return null;
    }

    @Override
    public List<EquipmentParamsDto> getEquipmentParams(QueryParamsDto dto) {
        List<EquipmentParamsDto> paramsDtos = Lists.newArrayList();
        // 复位状态
        int resetStatus = 0; // PlcToolKit.readSingleRegister(dto.getIp(), dto.getPort(), 2);
        paramsDtos.add(new EquipmentParamsDto("复位状态", "reset", String.valueOf(resetStatus), resetStatus == 1 ? "复位完成" : "未完成"));
        int stopStatus =0; // PlcToolKit.readSingleRegister(dto.getIp(), dto.getPort(), 3);
        paramsDtos.add(new EquipmentParamsDto("急停", "stopStatus", String.valueOf(stopStatus), stopStatus == 1 ? "触发" : "未触发"));

        int match = 0; //PlcToolKit.readSingleRegister(dto.getIp(), dto.getPort(), 13);
        paramsDtos.add(new EquipmentParamsDto("模组对接信号", "match", String.valueOf(match), match == 1 ? "对接完成" : "未完成"));
        int door = 0; //PlcToolKit.readSingleRegister(dto.getIp(), dto.getPort(), 11);
        paramsDtos.add(new EquipmentParamsDto("取油柜门状态", "door", String.valueOf(door), door == 1 ? "开" : "关"));

        int matchExit = 0; //PlcToolKit.readSingleRegister(dto.getIp(), dto.getPort(), 17);
        paramsDtos.add(new EquipmentParamsDto("对接完成退出", "matchExit", String.valueOf(matchExit), matchExit== 1 ? "完成":"未完成"));
        if (matchExit == 1) {
            PlcToolKit.writeRegister(dto.getIp(), dto.getPort(), 16, 0);
        }

        int fetOil = 0; //PlcToolKit.readSingleRegister(dto.getIp(), dto.getPort(), 15);
        paramsDtos.add(new EquipmentParamsDto("取油阀门状态", "fetOil", String.valueOf(fetOil), fetOil== 1 ? "完成":"未完成"));
        if (fetOil == 1) {
            PlcToolKit.writeRegister(dto.getIp(), dto.getPort(), 15, 0);
        }


        return paramsDtos;
    }

    @Override
    public List<EquipmentParamsDto> getDeviceParamsData(QueryParamsDto dto, EquipmentCmdEnum cmdEnum) {


        return Lists.newArrayList();
    }

    @Override
    public IEquipmentCmdResult getExecuteCmdResult(EquipmentCmdEnum cmdEnum) {
        return IEquipmentService.super.getExecuteCmdResult(cmdEnum);
    }

    @Override
    public boolean setEquipmentParams(QueryParamsDto dto, List<EquipmentParamsDto> paramsDtos, boolean async) {


        return true;
    }

    @Override
    public FormConfigDto getEquipmentConfig() {
        ArrayList<FormFieldConfigDto> fields = new ArrayList<>();
        fields.add(RobotParamsFormFieldEnum.PLC_WASTE_OIL.toConfigDto());
        fields.add(RobotParamsFormFieldEnum.PLC_FET_OIL.toConfigDto());
        // 通过 FormConfigEnum 创建表单配置
        return FormConfigEnum.ROBOT_PARAMS_CONFIG.toFormConfigDto(fields);
    }

}
